package com.ruoyi.api.controller;

import com.ruoyi.api.annotation.Login;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.vo.ObsUploadRespVo;
import com.ruoyi.common.utils.oss.OssUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;


@Api(tags = "文件上传")
@AllArgsConstructor
@RestController
@RequestMapping("/api/obs")
public class AppObsController {
    private final OssUtils ossUtils;

    @ApiOperation("文件上传")
    @PostMapping("/upload")
    @Login
    public AjaxResult<ObsUploadRespVo> upload(@RequestParam(value="file") MultipartFile file) throws IOException {
        return AjaxResult.success(ossUtils.uploadMultipartFile(file));
    }

    @ApiOperation("文件下载")
    @PostMapping("/download")
    @Login
    public void download(@ApiParam(value = "objectkey",required = true) @RequestParam("objectKey") String objectKey){
        ossUtils.downFile(objectKey);
    }

    @ApiOperation("删除文件")
    @PostMapping("/delFile")
    @Login
    public AjaxResult delFile(@ApiParam(value = "objectkey",required = true) @RequestParam("objectKey") String objectKey) {
        ossUtils.deleteFile(objectKey);
        return AjaxResult.success();
    }
}
